<script setup lang="ts">
// 引入three.js
import * as THREE from 'three';

import {onMounted, ref} from "vue";

const canvesRef = ref<HTMLCanvasElement | null>(null);
// 浏览器视窗宽度
const width = ref(400);
const height = ref(350);

const init = () => {
  const scene = new THREE.Scene();

  // scene.add(camera);
  const gemetry = new THREE.BoxGeometry(100, 100, 100);
  const material = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
  const mesh = new THREE.Mesh(gemetry, material);
  mesh.position.set(0, 0, 0);
  scene.add(mesh)

  const camera = new THREE.PerspectiveCamera(75, width.value / height.value, 0.1, 1000);
  camera.position.set(200, 200, 200)
  camera.lookAt(0, 10, 0);

  const renderer = new THREE.WebGLRenderer({
    antialias: true,
  });
  renderer.setSize(width.value, height.value);

  const pointLight = new THREE.PointLight(0xffffff, 1);//点光源位置
  pointLight.position.set(400, 0, 0);//点光源放在x轴上
  scene.add(pointLight);
  renderer.render(scene, camera);


  // AxesHelper：辅助观察的坐标系
  const axesHelper = new THREE.AxesHelper(150);
  scene.add(axesHelper);


  canvesRef.value?.appendChild(renderer.domElement);
}

onMounted(() => {
  console.log('game init in mounted')
  init();
})
</script>

<template>
  <div class="game-wrapper">
    <div class="title">跳一跳</div>
    <div ref="canvesRef" class="canves-wrapper"></div>
  </div>
</template>

<style scoped>
.game-wrapper {
  width: 100%;
  height: 100%;
  background-color: #000;
  position: relative;
}
.title {
  position: absolute;
  width: 100%;
  height: 40px;
  text-align: center;
}
.canves-wrapper {
  position: absolute;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  width: 400px;
  height: 300px;
}
</style>
